﻿namespace RM.Weighing.Models
{
    public class PrintLabel
    {
        public static GodexPrinter Printer = new GodexPrinter();

        public static bool Print(WeighingRecord weighingRecord, string PrintName)
        {
            Printer.Open(PrintName);
            string qrcode = weighingRecord.ProductCode + weighingRecord.RegisterDate + weighingRecord.SerialNumber;
            string text = Program.labelPrint;
            text = text.Replace("$productname", weighingRecord.ProductName.Trim());
            text = text.Replace("$weighing", weighingRecord.WeiGhing.Replace(" ", ""));
            text = text.Replace("$qrcode", qrcode);
            text = text.Replace("$weightstandard", weighingRecord.WeiGhingStandard);
            string[] strings = text.Split(new char[] { '\r', '\n' });
            for (int i = 0; i < strings.Length; i++)
            {
                if (strings[i].Length == 0)
                {
                    continue;
                }
                Printer.Command.Send(strings[i]);
            }
            Printer.Close();
            return true;
        }

        public static string utf8_gb2312(string text)
        {
            //声明字符集   
            System.Text.Encoding utf8, gb2312;
            //utf8   
            utf8 = System.Text.Encoding.GetEncoding("utf-8");
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            byte[] utf;
            utf = gb2312.GetBytes(text);
            utf = System.Text.Encoding.Convert(gb2312, utf8, utf);
            //返回转换后的字符   
            return utf8.GetString(utf);
        }

    }
}
